downloads = function() {
	//定义添加修改窗体
	var DOWNLOADADDOREDITWIN = null;
	//定义CKEDITR组件
	var DOWNLOADCKEDITTXTCOM = null;
	//定义搜索窗体
	var DOWNLOADSEARCHWIN = null;
	//文件上传窗体
	var DWONLOADFILEUPLOADWIN = null;
	//定义状态STORE
	var DOWNLOADSTATESTORE = new Store({
		fields: ['id', 'str'],
		data: [
		   { id: 1, str: '显示' },
		   { id: 0, str: '隐藏' }
		   
		]
	});
	//定义栏目STORE
	var DOWNLOADCHANNELSTORE = new Store({
		fields: ['id', 'str'],
		proxy: {
			type: 'ajax',
	        url: '../admin/download.do?indextochannel'
		},
		autoLoad: false
	});
	
	//定义用户STORE
	var DOWNLOADUSERSTORE = new Store({
		fields: ['id', 'str'],
		proxy: {
			type: 'ajax',
	        url: '../admin/download.do?indextouser'
		},
		autoLoad: false
	});
	
	return {
		/*--------------------------------------窗体定义--------------------------------------------*/
		//定义GRID窗体
		panel : function() {
			//初始化数据
			DOWNLOADCHANNELSTORE.load();
			DOWNLOADUSERSTORE.load();
			
			var store = new Store({
				fields: ['id', 'title', 'releaseDate', 'updateDate',
				         'level', 'txt', 'state', 'channel', 'user'],
				proxy: {
			        type: 'ajax',
			        url: '../admin/download.do?index',
			        reader: {
			            type: 'json',
			            root: 'datas',
			            totalProperty: 'total'
			        }
			    }
			});
			
			store.removeAll();
			store.currentPage = 1;
			store.proxy.extraParams = {
				title: '',
				channel: -1,
				state: -1
			};
			store.load();
			
			var pageToolBar = new PagingToolbar({
	            store: store,
	            draggable : false,
	            displayInfo: true
	        });
			
			var grid = new Grid({
				title: '下载中心列表',
				id: 'downloadgrid',
				region: 'center',
				store: store,
				minColumnWidth: 35,
				stripeRows: true,
				bbar : pageToolBar,
				tbar: [{
					text: '刷新',
					scale: 'medium',
					iconCls: 'icon-refresh',
					handler: function() {
						store.load();
					}
				}, "-", {
					text: '搜索',
					scale: 'medium',
					iconCls: 'icon-search',
					handler: function() {
						downloads.searchAction();
					}
				}, "-", {
					id: 'downloadAdd',
					text: '添加',
					scale: 'medium',
					iconCls: 'icon-add',
					disabled: true,
					handler: function() {
						downloads.addAction();
					}
				}, "-", {
					id: 'downloadEdit',
					text: '修改',
					scale: 'medium',
					iconCls: 'icon-edit',
					disabled: true,
					handler: function() {
						downloads.editAction();
					}
				}, "-", {
					id: 'downloadRemove',
					text: '删除',
					scale: 'medium',
					iconCls: 'icon-remove',
					disabled: true,
					handler: function() {
						downloads.removeAction();
					}
				}],
				columns: [
		             new Ext.grid.RowNumberer(),
		             {header: '序号', dataIndex: 'id', hidden: true},
		             {header: '标题', dataIndex: 'title',flex: 1},
		             {header: '栏目', dataIndex: 'channel', width: 150, renderer: function(v) {
		            	 return DOWNLOADCHANNELSTORE.getById(v).get('str'); 
		             }},
		             {header: '状态', dataIndex: 'state', width: 100, renderer: function(v) {
		            	 return DOWNLOADSTATESTORE.getById(v).get('str'); 
		             }},
		             {header: '优先级', dataIndex: 'level', width: 100},
		             {header: '创建者', dataIndex: 'user', width: 150, renderer: function(v) {
		            	 
		            	 return DOWNLOADUSERSTORE.getById(v).get('str'); 
		             }},
		             {header: '发布时间', dataIndex: 'releaseDate', width: 150},
		             {header: '更新时间', dataIndex: 'updateDate', width: 150}
				]
			});
			
			var panel = new Panel({
				border: false,
				layout: 'border',
				items: [grid]
			});
			return panel;
		},
		
		//定义搜索窗体
		searchWin: function() {
			if (DOWNLOADSEARCHWIN == null) {
				var channelStore = new Store({
					fields: ['id', 'str'],
					data: [
					   {'id': -1, 'str': '全部'}
					]
				});
				DOWNLOADCHANNELSTORE.each(function(v) {
					channelStore.add(v);
				});
				
				var stateStore = new Store({
					fields: ['id', 'str'],
					data: [
					   { id: -1, str: '全部' },
					   { id: 1, str: '显示' },
					   { id: 0, str: '隐藏' }
					   
					]
				});
				
				DOWNLOADSEARCHWIN = new Window({
					id: 'downloadSearchWin',
				    title: '搜索下载',
				    height: 200,
				    width: 450,
				    layout: 'fit',
				    closeAction: "hide",
				    modal: true,
				    items: [{
				    	xtype: 'form',
				    	frame: false,
						autoHeight: true,
						autoScroll: true,
						bodyPadding: 15,
						fieldDefaults: {
				            labelWidth: 75
				        },
				        defaultType: 'textfield',
				        defaults: {
				            anchor: '95%'
				        },
				        items: [{
				        	fieldLabel: '标题',
				        	name: 'title'
				        }, {
				        	fieldLabel: '栏目',
				        	xtype: 'combo',
			             	name: 'channel',
			             	store: channelStore,
			                displayField: 'str',
			                valueField: 'id',
			                queryMode: 'local',
			                editable: false,
			                value: -1
				        }, {
				        	fieldLabel: '状态',
				        	xtype: 'combo',
			             	name: 'state',
			             	store: stateStore,
			                displayField: 'str',
			                valueField: 'id',
			                queryMode: 'local',
			                editable: false,
			                value: -1
				        }],
				        buttons: [{
				        	text: '确定',
				        	handler: function() {
				        		var fom = this.up('form').getForm();
				        		downloads.searchSureAction(fom);
				        	}
				        }, {
				        	text: '取消',
				        	handler: function() {
				        		DOWNLOADSEARCHWIN.hide();
				        	}
				        }]
				    }],
				    listeners: {
			        	hide: function(fn, eOpts) {
			        		this.down('form').getForm().reset();
			        	}
			        }
				});
			}
			DOWNLOADSEARCHWIN.show();
		},
		
		//定义添加修改窗体
		addOrEditWin: function() {
			var store = new Store({
				fields: ['name', 'url']
			});
			
			if (DOWNLOADADDOREDITWIN == null) {
				DOWNLOADADDOREDITWIN = new Window({
					id: 'downloadAddOrEditWin',
				    title: '下载中心信息编辑',
				    height: 550,
				    width: 1000,
				    layout: 'fit',
				    closeAction: "hide",
				    modal: true,
				    items: [{
				    	xtype: 'form',
				    	frame: false,
						autoHeight: true,
						autoScroll: true,
						bodyPadding: 15,
						fieldDefaults: {
				            labelWidth: 75
				        },
				        defaultType: 'textfield',
				        defaults: {
				            anchor: '95%'
				        },
				        items: [{
				        	name: 'id',
				        	hidden: true
				        }, {
				        	fieldLabel: '标题',
				        	name: 'title',
				            allowBlank: false
				        }, {
				        	xtype: 'fieldcontainer',
				        	layout: 'hbox',
				        	fieldDefaults: {
					            labelWidth: 75
					        },
				        	items: [{
					        	fieldLabel: '发布时间',
					        	xtype: 'datetimefield',
					        	name: 'releaseDate',
					    		format: 'Y-m-d H:i:s',
					    		style: 'margin-right: 10px',
					    		value: new Date(),
					        	flex: 1
					        }, {
					        	fieldLabel: '优先级',
					        	xtype: 'numberfield',
					    		name: 'level',
					    		allowBlank: false,
					    		regex : /^-?[0-9]\d*$/,
					    	    regexText:'请输入有效的整数',
					    	    value: 0,
					        	flex: 1
					        }]
				        }, {
				        	xtype: 'fieldcontainer',
				        	layout: 'hbox',
				        	fieldDefaults: {
					            labelWidth: 75
					        },
				        	items: [{
				        		fieldLabel: '栏目',
					        	xtype: 'combo',
				             	name: 'channel',
				             	style: 'margin-right: 10px',
				             	store: DOWNLOADCHANNELSTORE,
				                displayField: 'str',
				                valueField: 'id',
				                queryMode: 'local',
				                editable: false,
					            allowBlank: false,
					        	flex: 1
					        }, {
					        	fieldLabel: '状态',
					        	xtype: 'combo',
				             	name: 'state',
				             	store: DOWNLOADSTATESTORE,
				                displayField: 'str',
				                valueField: 'id',
				                queryMode: 'local',
				                editable: false,
					            allowBlank: false,
					        	flex: 1
					        }]
				        }, {
				        	xtype: 'fieldcontainer',
				        	layout: 'fit',
					        fieldLabel: '下载资源',
					        items: [{
					        	id: 'downloadfileupload',
					        	xtype: 'gridpanel',
					        	store: store,
					        	height: 150,
					        	tbar: [{
					        		text: '添加',
									scale: 'small',
									iconCls: 'icon-add',
									handler: function() {
										downloads.fileUploadAction();
									}
					        	}, "-", {
					        		text: '删除',
					        		scale: 'small',
					        		iconCls: 'icon-remove',
					        		handler: function() {
					        			downloads.removeImgAction();
					        		}
					        	}],
					        	columns: [
				        	          { header: '文件名称',  dataIndex: 'name', flex: 1  },
				        	          { header: '资源路径', dataIndex: 'url', flex: 1}
				        	      ]
					        }]
				        }, {
				        	xtype: 'textareafield',
				    		name: 'txt',
				    		hidden: true
				        }, {
				    		xtype: 'fieldcontainer',
				            fieldLabel: '内容',
				            layout: 'fit',
				            height: 330,
				            items: [{
				    	    	xtype: 'panel',
				    	    	border: false,
				    	    	html: '<textarea id="_downloadfomtxt" class="ckeditor" name="_downloadfomtxt"></textarea>',
				    	    	height: 330
				    		}]
				        }],
				        buttons: [{
				        	text: '确定',
				        	handler: function() {
				        		var fom = this.up('form').getForm();
				        		if (fom.isValid()) {
				        			fom.findField('txt').setValue(DOWNLOADCKEDITTXTCOM.getData());
				        			downloads.sumbitAction(fom);
				        		}
				        	}
				        }, {
				        	text: '取消',
				        	handler: function() {
				        		DOWNLOADADDOREDITWIN.hide();
				        	}
				        }],
				        listeners: {
				        	afterrender: function() {
				        		DOWNLOADCKEDITTXTCOM = CKEDITOR.replace('_downloadfomtxt');
				        	},
				        	beforedestroy: function() {
				        		DOWNLOADCKEDITTXTCOM.destory();
				        	}
				        }
				    }],
			        listeners: {
			        	hide: function(fn, eOpts) {
			        		this.down('form').getForm().reset();
			        		DOWNLOADCKEDITTXTCOM.setData('');
			        		Ext.getCmp('downloadfileupload').getStore().removeAll();
			        	}
			        }
				});
			}
			DOWNLOADADDOREDITWIN.show();
		},
		
		//定义文件上传窗体
		fileUploadWin: function() {
			if (DWONLOADFILEUPLOADWIN == null) {
				DWONLOADFILEUPLOADWIN = new Window({
					id: 'downloadFileUploadWin',
				    title: '文件资源上传',
				    height: 160,
				    width: 450,
				    layout: 'fit',
				    closeAction: "hide",
				    modal: true,
				    items: [{
				    	xtype: 'form',
				    	frame: false,
						autoHeight: true,
						autoScroll: true,
						bodyPadding: 15,
						fieldDefaults: {
				            labelWidth: 75,
				            anchor: '95%'
				        },
				        items: [{
				        	xtype: 'textfield',
				        	fieldLabel: '文件名称',
				        	name: 'title'
				        }, {
				        	fieldLabel: '文件资源',
				        	xtype: 'filefield',
				        	name: 'fileupload',
				        	buttonText: '选择',
				        	allowBlank: false
				        }],
				        buttons: [{
				        	text: '确定',
				        	handler: function() {
				        		var fom = this.up('form').getForm();
				        		if (fom.isValid()) {
				        			downloads.uploadSureAction(fom);
				        		}
				        	}
				        }, {
				        	text: '取消',
				        	handler: function() {
				        		DWONLOADFILEUPLOADWIN.hide();
				        	}
				        }]
				    }],
			        listeners: {
			        	hide: function(fn, eOpts) {
			        		this.down('form').getForm().reset();
			        	}
			        }
				});
			}
			DWONLOADFILEUPLOADWIN.show();
		},
		
		/*--------------------------------------事件定义--------------------------------------------*/
		//搜索窗体事件
		searchAction: function() {
			downloads.searchWin();
		},
		
		//搜索确定事件
		searchSureAction: function(fom) {
			var store = Ext.getCmp('downloadgrid').getStore();
			store.removeAll();
			store.currentPage = 1;
			store.proxy.extraParams = {
				title: fom.findField('title').value,
				channel: fom.findField('channel').value,
				state: fom.findField('state').value
			};
			store.load();
			DOWNLOADSEARCHWIN.hide();
		},
		
		//添加窗体事件
		addAction: function() {
			downloads.addOrEditWin();
		},
		
		//修改窗体事件
		editAction: function() {
			var sel = Ext.getCmp('downloadgrid').getSelectionModel().getSelection();
			if (sel.length < 1) {
				Ext.Msg.alert('提示', '请选择要修改的信息！');
			} else {
				downloads.addOrEditWin();
				var fom = Ext.getCmp('downloadAddOrEditWin').down('form').getForm();
				fom.loadRecord(sel[0]);
				DOWNLOADCKEDITTXTCOM.setData(fom.findField('txt').value);
				var store = Ext.getCmp('downloadfileupload').getStore();
				var files = RequestByUrl('../admin/download.do?indextodetail', {
					id: sel[0].get('id')
				});
				for (var i = 0; i < files.length; i++) {
					store.add({name: files[i].title, url: files[i].url});
				}
			}
		},
		
		//提交确认事件
		sumbitAction: function(fom) {
			var url = '../admin/download.do?edit';
			var id = fom.findField('id').value;
			if (id == '') {
				url = '../admin/download.do?add';
			}
			var store = Ext.getCmp('downloadfileupload').getStore();
			var i = 0;
			var sources = new Array();
			store.each(function(v) {
				sources[i++]= v.get('name') + ';' + v.get('url');
			});
			FormSubmit(fom, url, {
				sources: sources
			}, function(obj) {
				Ext.getCmp('downloadgrid').getStore().load();
				DOWNLOADADDOREDITWIN.hide();
			});
		},
		
		//删除事件
		removeAction: function() {
			var sel = Ext.getCmp('downloadgrid').getSelectionModel().getSelection();
			if (sel.length < 1) {
				Ext.Msg.alert('提示', '请选择要删除的信息！');
			} else {
				Ext.MessageBox.confirm('确认', '您确认要删除该信息吗？', function(btn) {
					if (btn == "yes") {
						var res = RequestByUrl('../admin/download.do?remove', {
							id: sel[0].get('id')
						});
						Ext.getCmp('downloadgrid').getStore().load();
						Ext.Show.msg(res.title, '');
					}
				});
			}
		},
		
		//文件上传窗体
		fileUploadAction: function() {
			downloads.fileUploadWin();
		},
		
		//文件上传确定事件
		uploadSureAction: function(fom) {
			var store = Ext.getCmp('downloadfileupload').getStore();
			FormSubmit(fom, '../admin/download.do?indextoimg', {}, function(obj) {
				if (obj.title == '上传成功！') {
					store.add({name: fom.findField('title').value, url: obj.msg});
					Ext.Show.msg(obj.title, '');
					DWONLOADFILEUPLOADWIN.hide();
				} else {
					Ext.Show.msg(obj.title, obj.msg);
				}
			}, true);
		},
		
		//删除文件事件
		removeImgAction: function() {
			var sel = Ext.getCmp('downloadfileupload').getSelectionModel().getSelection();
			if (sel.length < 1) {
				Ext.Msg.alert('提示', '请选择要删除的文件资源！');
				return false;
			} else {
				Ext.MessageBox.confirm('确认', '您确认要删除该文件资源吗？', function(btn) {
					if (btn == "yes") {
						var store = Ext.getCmp('downloadfileupload').getStore();
						store.remove(sel[0]);
						Ext.Show.msg('删除成功！', '');
					}
				});
			}
		}
	};
}();